package com.shrct.businessprocess.budget.impl;

import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.budget.RCSaleContractPayCondition;
import com.shrct.businessprocess.budget.proxy.RCSaleContractPayConditionProxy;
import com.shrct.businessprocess.common.RCSaleContractPayConditionCode;
import com.shrct.businessprocess.common.impl.RCSaleContractPayConditionCodeImpl;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.impl.RCProjectImpl;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCSALECONTRACTPAYCONDITION_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcsalecontractpaycondition")
public class RCSaleContractPayConditionImpl extends BaseModelObject implements RCSaleContractPayCondition {
  public RCSaleContractPayConditionImpl() {
    super();
  }
  
  public RCSaleContractPayConditionImpl(final RCProject project) {
    super();
    this.project = project;
  }
  
  @JoinColumn(name = "code_id")
  @ManyToOne(targetEntity = RCSaleContractPayConditionCodeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSaleContractPayConditionCode code;
  
  public RCSaleContractPayConditionCode getCode() {
    return this.code;
  }
  
  public RCSaleContractPayCondition setCode(final RCSaleContractPayConditionCode code) {
    this.code = code;
    return this;			
    
  }
  
  private Double payPrecent;
  
  public Double getPayPrecent() {
    return this.payPrecent;
  }
  
  public RCSaleContractPayCondition setPayPrecent(final Double payPrecent) {
    this.payPrecent = payPrecent;
    return this;			
    
  }
  
  private Integer deliverDate;
  
  public Integer getDeliverDate() {
    return this.deliverDate;
  }
  
  public RCSaleContractPayCondition setDeliverDate(final Integer deliverDate) {
    this.deliverDate = deliverDate;
    return this;			
    
  }
  
  @JoinColumn(name = "project_id")
  @ManyToOne(targetEntity = RCProjectImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCProject project;
  
  public RCProject getProject() {
    return this.project;
  }
  
  public RCSaleContractPayCondition setProject(final RCProject project) {
    this.project = project;
    return this;			
    
  }
  
  /**
   * 计算超期资金占用费
   */
  public RCProjectMoney getCBDEfc() {
    RCProjectMoney _xblockexpression = null;
    {
      RCCostBreakDown _cbd = this.project.getCbd();
      RCProjectMoney _contractPrice = _cbd.getContractPrice();
      Double _hostAmount = _contractPrice.toHostAmount();
      double _multiply = ((_hostAmount).doubleValue() * 0.12);
      final double dayAmount = (_multiply / 365);
      Collection<RCSaleContractPayCondition> _payConditions = this.project.getPayConditions();
      final Function1<RCSaleContractPayCondition,Boolean> _function = new Function1<RCSaleContractPayCondition,Boolean>() {
          public Boolean apply(final RCSaleContractPayCondition it) {
            RCSaleContractPayConditionCode _code = it.getCode();
            String _codeid = _code.getCodeid();
            String _codeid_1 = RCSaleContractPayConditionImpl.this.code.getCodeid();
            int _compareTo = _codeid.compareTo(_codeid_1);
            boolean _lessEqualsThan = (_compareTo <= 0);
            return Boolean.valueOf(_lessEqualsThan);
          }
        };
      final Iterable<RCSaleContractPayCondition> prevAndThisConditions = IterableExtensions.<RCSaleContractPayCondition>filter(_payConditions, _function);
      Collection<RCSaleContractPayCondition> _payConditions_1 = this.project.getPayConditions();
      final Function1<RCSaleContractPayCondition,Boolean> _function_1 = new Function1<RCSaleContractPayCondition,Boolean>() {
          public Boolean apply(final RCSaleContractPayCondition it) {
            RCSaleContractPayConditionCode _code = it.getCode();
            String _codeid = _code.getCodeid();
            String _codeid_1 = RCSaleContractPayConditionImpl.this.code.getCodeid();
            int _compareTo = _codeid.compareTo(_codeid_1);
            boolean _lessThan = (_compareTo < 0);
            return Boolean.valueOf(_lessThan);
          }
        };
      final Iterable<RCSaleContractPayCondition> prevConditions = IterableExtensions.<RCSaleContractPayCondition>filter(_payConditions_1, _function_1);
      final Function1<RCSaleContractPayCondition,Double> _function_2 = new Function1<RCSaleContractPayCondition,Double>() {
          public Double apply(final RCSaleContractPayCondition it) {
            Double _payPrecent = it.getPayPrecent();
            return _payPrecent;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCSaleContractPayCondition, Double>map(prevConditions, _function_2);
      final Function2<Double,Double,Double> _function_3 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_3);
      Double _nullsafe = NumberExtensions.nullsafe(_reduce);
      final double remainPercent = (100 - (_nullsafe).doubleValue());
      final Function1<RCSaleContractPayCondition,Integer> _function_4 = new Function1<RCSaleContractPayCondition,Integer>() {
          public Integer apply(final RCSaleContractPayCondition it) {
            Integer _deliverDate = it.getDeliverDate();
            return _deliverDate;
          }
        };
      Iterable<Integer> _map_1 = IterableExtensions.<RCSaleContractPayCondition, Integer>map(prevAndThisConditions, _function_4);
      final Function2<Integer,Integer,Integer> _function_5 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer _reduce_1 = IterableExtensions.<Integer>reduce(_map_1, _function_5);
      Integer _nullsafe_1 = NumberExtensions.nullsafe(_reduce_1);
      final int rawTotalDays = ((_nullsafe_1).intValue() - 120);
      RCProjectMoney _efc = this.project.getEfc(Double.valueOf(dayAmount), Double.valueOf(remainPercent), Integer.valueOf(rawTotalDays));
      _xblockexpression = (_efc);
    }
    return _xblockexpression;
  }
  
  public static RCSaleContractPayCondition create(final RCProject project, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.budget.RCSaleContractPayCondition rCSaleContractPayCondition = new com.shrct.businessprocess.budget.impl.RCSaleContractPayConditionImpl(
    	project
    );
    objectFactory.create(rCSaleContractPayCondition);
    return rCSaleContractPayCondition;			
    
  }
  
  public static RCSaleContractPayCondition createRCSaleContractPayConditionImpl(final RCProject project, final IObjectFactory objectFactory, final Procedure1<RCSaleContractPayCondition> init) {
    com.shrct.businessprocess.budget.RCSaleContractPayCondition rCSaleContractPayCondition = new com.shrct.businessprocess.budget.impl.RCSaleContractPayConditionImpl(
    	project
    );
    objectFactory.create(rCSaleContractPayCondition);
    init.apply(rCSaleContractPayCondition);
    
    return rCSaleContractPayCondition;			
    
  }
  
  public RCSaleContractPayConditionProxy toProxy() {
    com.shrct.businessprocess.budget.proxy.RCSaleContractPayConditionProxy proxy = new com.shrct.businessprocess.budget.proxy.RCSaleContractPayConditionProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
